package com.cynteka.cyfoman.core.collection.scxml;

import org.apache.commons.logging.Log;
import org.apache.commons.scxml.*;
import org.apache.commons.scxml.model.Action;
import org.apache.commons.scxml.model.ModelException;

import java.util.Collection;
import java.util.Map;

/**
 * User: Sergey
 * Date: 10.08.11
 * Time: 7:30
 */
public class AssignAction extends Action {

    private String property;
    private String value;

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public void execute(EventDispatcher evtDispatcher, ErrorReporter errRep, SCInstance scInstance, Log appLog, Collection derivedEvents) throws ModelException, SCXMLExpressionException {
        Context ctx = scInstance.getContext(getParentTransitionTarget());
        Evaluator eval = scInstance.getEvaluator();
        if(value.startsWith("${"))
            value = value.substring(2);
        if(value.endsWith("}"))
            value = value.substring(0, value.length() - 1);
        Object oValue = eval.eval(ctx, value);
        Map vo = (Map) ctx.get(SCXMLConstants.THIS);
        vo.put(property, oValue);
    }
}
